package com.mql.onlinechatsystem.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.mql.onlinechatsystem.system.entity.SysUser;
import com.mql.onlinechatsystem.system.entity.TbUserrelation;
import com.mql.onlinechatsystem.system.mapper.SysUserMapper;
import com.mql.onlinechatsystem.system.mapper.TbUserrelationMapper;
import com.mql.onlinechatsystem.system.service.ISysUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mql.onlinechatsystem.system.service.ITbUserrelationService;
import com.mql.onlinechatsystem.system.vo.NewFriendVo;
import com.mql.onlinechatsystem.system.vo.TbUserrelationVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 马庆林
 * @since 2023-03-07
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {

    @Autowired
    private TbUserrelationMapper tbUserrelationMapper;
    @Override
    public List<NewFriendVo> getNewFriend(TbUserrelationVo tbUserrelationVo) {
        List<SysUser> sysUserList = baseMapper.selectList(new QueryWrapper<SysUser>()
                .like("username", tbUserrelationVo.getFriendname()));
        if (StringUtils.isBlank(tbUserrelationVo.getFriendname())){
            return new ArrayList<>();
        }
        List<NewFriendVo> collect = sysUserList.stream().map(item -> {
            NewFriendVo newFriendVo = new NewFriendVo();
            BeanUtils.copyProperties(item, newFriendVo);
            TbUserrelation selectOne = tbUserrelationMapper.selectOne(new QueryWrapper<TbUserrelation>().eq("userid", tbUserrelationVo.getUserid())
                    .eq("friendid", item.getUserid()));
            if (selectOne != null) {
                newFriendVo.setHaving(true);
            }
            return newFriendVo;
        }).collect(Collectors.toList());
        return collect;
    }
}
